<script type="text/javascript">
    function create_news(){
        var news_title = document.getElementById('news_title').value;
        var news_image = document.getElementById('news_image').value;
        var news_content = tinyMCE.get('news_content').getContent();
        var countError = 0;
        if((news_title == '')||(news_title.length > 300))
        {
            document.getElementById('errorNewsTitle').innerHTML = 'Tiêu đề tin không được để trống và tối đa 300 kí tự';
            countError++;
        }
        else
        {
            document.getElementById('errorNewsTitle').innerHTML = '';
        }
        if(news_image == '')
        {
            document.getElementById('errorNewsImage').innerHTML = 'Ảnh phải đúng định dạng (jpeg, jpg, bmp, png, gif) và dung lượng tối đa 400KB';
            countError++;
        }
        else
        {
            document.getElementById('errorNewsImage').innerHTML = '';
        }
        if(news_content == '')
        {
            document.getElementById('errorNewsContent').innerHTML = 'Nội dung tin không được để trống';
            countError++;
        }
        else
        {
            document.getElementById('errorNewsContent').innerHTML = '';
        }
        if(countError > 0)
        {
            return false;
        }
        else
        {
            content = tinyMCE.get('news_content').getContent({format : 'raw'});
            re = /<[a-zA-Z\/][^>]*>/ig;
            content = content.replace(re," ");
            re = /&\w+;/ig;
            content = content.replace(re," ");   
            content = content.replace(/\s{2,}/," ");
            //Luu gia tri plain text vao truong an. Sau khi submit ve server, lay gia tri
            //truong an ra de luu vao mot truong du lieu ban ro cua noi dung, phuc vu tim kiem
            
            document.getElementById('news_plaintext').value = content;
            return true;
        }
    }
</script>
<div class="heading">
    <div class="left_heading"></div>
                                
    <div class="content_heading">
        <h3>Thêm tin</h3>
    </div>
                                
    <div class="right_heading"></div>
</div><!-- End .heading -->

<div class="main_content">
    <form action="" method="post" enctype="multipart/form-data" onsubmit="return create_news()">
        <input type="hidden" name="news_plaintext" id="news_plaintext" />
        
        <table class="table_content">
			<tr>
				<td class="label"><?php echo $this->formLabel('news_title', 'Tiêu đề'); ?><span class="msg"> * </span></td>
				<td class="label2"><?php echo $this->formText('news_title', $this->newsTitleValue, array('id' => 'news_title', 'class' => 'input')); ?></td>
				<td><span class="msg" id="errorNewsTitle"><?php echo $this->errorNewsTitle; ?></span></td>
			</tr>
            
			<tr>
				<td class="label"><?php echo $this->formLabel('news_image', 'Ảnh'); ?><span class="msg"> * </span></td>
				<td class="label2"><?php echo $this->formFile('news_image', array('id' => 'news_image', 'class' => 'input')); ?></td>
				<td><span class="msg" id="errorNewsImage"><?php echo $this->errorNewsImage; ?></span></td>
			</tr>
        </table>
        
        <table class="table_content">
            <tr>
                <td class="label"><?php echo $this->formLabel('news_content', 'Nội dung'); ?><span class="msg"> * </span></td>
            </tr>
            
            <tr>
                <td><?php echo $this->formTextarea('news_content', $this->newsContentValue, array('id' => 'news_content', 'cols' => '80', 'rows' => '30')); ?></td>
                <td><span class="msg" id="errorNewsContent"><?php echo $this->errorNewsContent; ?></span></td>
            </tr>
        </table>
        <table >
            <tr>
                <td class="label"><?php echo $this->formLabel('status','Trạng thái',array('class'=>'label1')) ;?></td>
                <td >
                  
                    <?php
                    echo $this->formSelect('status',null,array('id'=>'status','class'=>'input'),array('1'=>'Hiển thị','0'=>'Ẩn đi'));
                    ?>
                  
                </td>
            </tr>
        </table>
        <table class="table_content">
			<tr>
				<td class="label">&nbsp;</td>
				<td colspan="2"><span class="msg"><?php echo $this->error; ?></span></td>
			</tr>
			<tr>
				<td class="label">&nbsp;</td>
				<td colspan="2">
					<input class="button" type="submit" name="submit" value="Tạo mới"/>
					<input class="button" type="reset" name="reset" value="Nhập lại"/>
					<a href="<?php echo $this->baseUrl('admin/news'); ?>" class="button" style="padding-top: 7px;">Quay lại</a>
				</td>
			</tr>
        </table>
    </form>
</div><!-- End .main_content -->  